<?php

namespace App\Http\Controllers\Api;

use App\Classify;
use App\Course;
use App\CoursePackageXCourse;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\OrderItem;
use App\Teacher;
use App\TeachersXClassify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TeacherController extends Controller
{
    public function getTeacherClassify(Request $request){
        $allClassify = Classify::whereType("teacher")->orderBy("sort","asc")->get();

        return response()->json(['level' => 'success','message' => '获取成功','data' => $allClassify]);
    }
    /**
     * 获取教师列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTeacherList(Request $request){
        $limit      = $request->input('limit',10);
        $page       = $request->input('page',1);
        $classifyId = $request->input("classify_id",0);

        $pageInfo = [
            'limit'=> $limit,
            'page' =>$page
        ];

        $page -= 1;

        $teachArr = [];
        $teaches = Teacher::where("id",">",0);

        if(!is_null($classifyId) && $classifyId != 0){
            $teacherIds = TeachersXClassify::whereClassifyId($classifyId)->pluck("teachers_id")->toArray();

            $teaches = $teaches->whereIn("id",$teacherIds);
        }

        $teaches = $teaches->get();

        $pageInfo['total'] = $teaches->count();
        if(count($teaches) > 0){
            foreach ($teaches as $teach) {
                $courseIdArr = Course::whereTeacherId($teach->id)->pluck('id')->toArray();

                $courseCount = OrderItem::whereIn('course_id',$courseIdArr)->count();
                $teachArr[$teach->id] =  $courseCount;

            }
        }

        arsort($teachArr);

        $teachIds = array_keys($teachArr);

        $start = ($limit * $page);

        $teachIds = array_slice($teachIds,$start,$limit);

        $data = [];
        if(count($teachIds) > 0){
            foreach ($teachIds as $teachId) {
                $teacher = Teacher::whereId($teachId)->first();
                if($teacher){
                    $teacher->photo_url = FileManager::getImgUrl($teacher->photo_id);

                    $teacher->sex_name = $teacher->getSex($teacher->sex);

                    $courseIdArr = Course::whereTeacherId($teacher->id)->pluck('id')->toArray();

                    $courseCount = OrderItem::whereIn('course_id',$courseIdArr)->count();
                    $teacher->heat_degree = $courseCount + 100;

                    array_push($data,$teacher);
                }
            }
        }

//        /** @var Teacher $teacher */
//        $teacherList = Teacher::where('id','>',0);
//
//        $pageInfo['total'] = $teacherList->count();
//
//        $teacherList = $teacherList->offset($limit * $page)->limit($limit)->get();
//        if(count($teacherList) > 0){
//            foreach ($teacherList as $teacher) {
//                $teacher->photo_url = FileManager::getImgUrl($teacher->photo_id);
//
//                $teacher->sex_name = $teacher->getSex($teacher->sex);
//
//                $courseIdArr = Course::whereTeacherId($teacher->id)->pluck('id')->toArray();
//
//                $courseCount = CoursePackageXCourse::whereIn('course_id',$courseIdArr)->sum('sold_num');
//                $teacher->heat_degree = $courseCount + 100;
//            }
//        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $data,'page_info' => $pageInfo]);
    }
}
